{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bd9693dd",
   "metadata": {},
   "source": [
    "多维数组：获取numpy数组的维度和形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0d6775bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b3d30645",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "#获取数组的维度-一维数组\n",
    "a1=np.array([1,2,3])\n",
    "print(a1.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "af2fe8ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "#获取数组的维度-二维数组\n",
    "a2=np.array([[1,2,3],[4,5,6]])\n",
    "print(a2.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "91ce8430",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "#获取数组的维度-三维数组\n",
    "a3=np.array([\n",
    "            [[1,2,3],\n",
    "              [4,5,6]],\n",
    "             \n",
    "             [[7,8,9],\n",
    "             [4,5,6]]\n",
    "        ])\n",
    "print(a3.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d7ba29b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取数组ndarray的形状\n",
    "a1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4f785c0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7caecaae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "331ba307",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n",
      " [5 6 7 8]\n",
      " [9 4 5 6]]\n",
      "2\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "#数组形状的变形，三维以上的数组变形为低维数组\n",
    "\n",
    "a4 = a3.reshape(3,4)\n",
    "print(a4)\n",
    "print(a4.ndim)\n",
    "print(a4.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4650d7f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9 4 5 6]\n",
      "1\n",
      "(12,)\n"
     ]
    }
   ],
   "source": [
    "#reshape中有几个值，就是几维数组。直接获取一维数组时，在参数元组中写一个数即可。\n",
    "a5 = a3.reshape(12,)\n",
    "print(a5)\n",
    "print(a5.ndim)\n",
    "print(a5.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ca5d6ccb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9 4 5 6]\n",
      "1\n",
      "(12,)\n"
     ]
    }
   ],
   "source": [
    "#获取一维数组时，可以直接让ndarray扁平化\n",
    "#使用flatten函数\n",
    "a6 = a3.flatten()\n",
    "print(a6)\n",
    "print(a6.ndim)\n",
    "print(a6.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d5b298c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取数组中元素的总个数的方法size\n",
    "a3.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dae2a921",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2785aeef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "#获取数组中每个单元占用的字节数.每个字节占8位，返回值是8对应的int64数据格式\n",
    "print(a3.itemsize)\n",
    "print(a3.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afe198bf",
   "metadata": {},
   "source": [
    "# 总结：\n",
    "1. 数组达到三维就会很复杂，一般会把三维数组转化为二位数组计算\n",
    "2. 通过ndarray.ndim可以看到数组的维度；\n",
    "3. 通过ndarray.shape可以看到数组的形状，shape是一个元组，里面有几个元素代表几维数组\n",
    "4. 通过ndarray.reshape可以修改数组的形状。条件只有一个，修改前后的数据总数保持一致\n",
    "5. reshape不会修改原来数组，只是将修改后的数据返回"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
